$(function() {
	//菜单侧滑======================
	var menu = function(wrap, menuList, menuItems) {
		var deviceWidth = $(window).width();
		var positionX = 0;
		var menuListPositionX1 = wrap.offset().left;
		var menuListPositionX2 = menuListPositionX1 + wrap.width();

		$(menuList).attr("style", "transition-duration: 0ms;transform: translateX(0px);");

		menuList.addEventListener('touchstart', function(event) {
			if(event.targetTouches.length == 1) {
				var touch = event.targetTouches[0];
				positionX = touch.pageX;
				//确定本次拖动transform的初始值
				var transformStr = menuList.style.transform;
				transformStr = transformStr.substring(11);
				var index = transformStr.lastIndexOf("p");
				transformStr = transformStr.substring(0, index);
				transformX = parseInt(transformStr);
				//确定本次拖动的div宽度值
				var widthStr = menuList.style.width;
				thisWidth = parseInt(widthStr.substring(0, widthStr.lastIndexOf("p")));
			}
		}, false);
		menuList.addEventListener('touchmove', function(event) {
			//阻止其他事件
			//			event.preventDefault();
			//获取当前坐标
			if(event.targetTouches.length == 1) {
				var touch = event.targetTouches[0];
				menuList.style.transform = 'translateX(' + (transformX + touch.pageX - positionX) + 'px)';
				$(menuList).css("width", thisWidth + positionX - touch.pageX);
			}
		}, false);
		menuList.addEventListener('touchend', function(event) {
			var menuItem1 = menuItems[0];
			var menuItem1Left = $(menuItem1).offset().left;
			var menuItem2 = menuItems[menuItems.length - 1];
			var menuItemPositionX = $(menuItem2).offset().left + $(menuItem2).width();
			var firstToLast = menuItemPositionX - menuItem1Left;
			if(menuItem1Left > menuListPositionX1 || firstToLast < deviceWidth) {
				menuList.style.transform = 'translateX(' + (menuListPositionX1) + 'px)';
			}
			if(menuItemPositionX < menuListPositionX2 && menuItem1Left < 0 && firstToLast > deviceWidth) {
				var myWidth = $(menuList).width() - deviceWidth;
				menuList.style.transform = 'translateX(' + (0 - myWidth) + 'px)';
			}
		}, false);
	}
/*111111*/
	var event = function() {
		$('#menu-wrap .js-menu-list').click(function() {
			console.log($(this).attr('data-index'));
			var activeMenu = $('.js-menu-wrap' + $(this).attr('data-index'));
			activeMenu.siblings().hide();
			activeMenu.show();
		});
	}
	var init = function() {
		var menuWrap = $('#menu-wrap');
		var menuList = $("#menu-wrap .js-menu-list")[0];
		var menuListItems = $("#menu-wrap .menu-list-item");
		menu(menuWrap, menuList, menuListItems);

		var menuWrapLayer2 = $('.js-menu-wrap-layer2');
		var helper = function(tempWrap, tempMenuList, tempMenuListItems) {
			var tempFun = function() {
				menu(tempWrap, tempMenuList, tempMenuListItems);
			}();
		}
		for(var i = 0; i < menuWrapLayer2.length; i++) {
			var tempWrap = $(menuWrapLayer2[i]);
			var tempMenuList = $(menuWrapLayer2[i]).find('.js-menu-list')[0];
			var tempMenuListItems = $(menuWrapLayer2[i]).find('.menu-list-item');
			helper(tempWrap, tempMenuList, tempMenuListItems);
		}
		event();
	}();
});